<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

class admin_controller_user extends omnitrix_controller
{
	public function index_action()
	{
		$sql = 'SELECT u.*, g.group_name
				FROM #system_user# u
				LEFT JOIN #system_group# g ON g.group_id = u.group_id';
		extract(otx_db_list_query($sql, null, 'user_name', 'asc'));

		$list_table = otx_html_create_table($this->_('heading_index'), 'table-list', $pager);
		$list_table->set_head_cell($this->_('column_user_name'));
		$list_table->set_head_cell($this->_('column_display_name'));
		$list_table->set_head_cell($this->_('column_group_name'));
		$list_table->set_head_cell($this->_('column_email'));
		$list_table->set_head_cell($this->_('column_status'), array('class' => 'center'));
		$list_table->set_head_cell($this->_('column_last_visited_time'));
		$list_table->set_head_cell($this->_('column_last_visited_ip'));
		$list_table->set_head_cell($this->_('text_actions'), array('class' => 'center', 'width' => 100));

		if ($rows) {
			foreach ($rows as $row) {
				$actions = otx_html_tag_custom('dropdown');
				$actions->add_link(otx_html_link($this->_('action_edit'), array('href' => otx_router_url('edit', null, null, array('id' => $row['user_id'])))));
				$actions->add_link(otx_html_link($this->_('action_delete'), array('href' => otx_router_url('delete', null, null, array('id' => $row['user_id'])), 'data-confirm' => 'confirm_delete')));

				if ($row['status']) {
					$actions->add_link(otx_html_link($this->_('action_deactivate'), array('href' => otx_router_url('deactivate', null, null, array('id' => $row['user_id'])))));
				} else {
					$actions->add_link(otx_html_link($this->_('action_activate'), array('href' => otx_router_url('activate', null, null, array('id' => $row['user_id'])))));
				}

				$list_table->add_body_row();
				$list_table->set_body_cell(otx_html_link(otx_html_strong($row['user_name']), array('href' => otx_router_url('edit', null, null, array('id' => $row['user_id'])))));
				$list_table->set_body_cell(otx_html_link($row['display_name'], array('href' => otx_router_url('edit', null, null, array('id' => $row['user_id'])))));
				$list_table->set_body_cell(otx_html_link($row['group_name'], array('href' => otx_router_url('edit', 'group', null, array('id' => $row['group_id'])))));
				$list_table->set_body_cell(otx_html_link($row['email'], array('href' => 'mailto:' . $row['email'])));
				$list_table->set_body_cell($row['status'] ? $this->_('text_active') : otx_html_strong($this->_('text_deactive')), array('class' => 'center'));
				$list_table->set_body_cell(otx_string_format_datetime($row['last_visited_time']));
				$list_table->set_body_cell($row['last_visited_ip']);
				$list_table->set_body_cell($actions->render_link($this->_('text_options')), array('class' => 'center actions'));
			};
		} else {
			$list_table->add_body_row();
			$list_table->set_body_cell($this->_('text_no_results'), array('colspan' => 8));
		}

		otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'), true);
		otx_html_add_navtab($this->_('navtab_add_title'), $this->_('navtab_add_desc'), otx_router_url('add'));
		otx_html_add_breadcrumb($this->_('breadcrumb_index'));
		otx_html_response_title($this->_('breadcrumb_index'));
	}

	public function add_action()
	{
		$this->edit_action();
	}

	public function edit_action()
	{
		$user_id = otx_param_int('id');

		if ($user_id) {
			$sql = 'SELECT * FROM #system_user# WHERE user_id = ?';
			$row = otx_db_fetch_row($sql, $user_id);
			if (false == $row) {
				otx_admin_flash_warning($this->_('error_invalid_id', $user_id));
				otx_router_redirect('index');
			}
		}

		if (otx_request_is_post()) {
			$vars['user_name'] = otx_param_string('user_name');
			$vars['display_name'] = otx_param_string('display_name');
			$vars['email'] = otx_param_string('email');
			$vars['group_id'] = otx_param_int('group_id');
			$vars['password'] = otx_param_string('password');

			if (otx_validate_is_empty($vars['user_name'])) {
				$errors[] = $this->_('error_user_name_empty');
			} else {
				if (($user_id && otx_validate_field_count('#system_user#', 'user_name', $vars['user_name'], 'user_id <> ' . $user_id) > 0) ||
					(!$user_id && otx_validate_field_count('#system_user#', 'user_name', $vars['user_name']) > 0)) {
					$errors[] = $this->_('error_user_name_exists');
				}
			}

			if (otx_validate_is_empty($vars['display_name'])) {
				$errors[] = $this->_('error_display_name_empty');
			}

			if (otx_validate_is_empty($vars['group_id'])) {
				$errors[] = $this->_('error_group_id_empty');
			}

			if (otx_validate_is_empty($vars['email'])) {
				$errors[] = $this->_('error_email_empty');
			} else if (!otx_validate_is_email($vars['email'])) {
				$errors[] = $this->_('error_email_invalid');
			}

			if (empty($errors)) {
				if (otx_validate_is_empty($vars['password'])) {
					unset($vars['password']);
				} else {
					$vars['password'] = otx_string_crypt($vars['password']);
				}

				if ($user_id) {
					otx_db_update('#system_user#', $vars, array('user_id' => $user_id));

					$vars['user_id'] = $user_id;
					otx_admin_flash_message($this->_('text_success_edit', $vars));
					otx_admin_log_action($this->_('text_success_edit', $vars));
				} else {
					otx_db_insert('#system_user#', $vars);

					$vars['user_id'] = otx_db_last_inserted_id();
					otx_admin_flash_message($this->_('text_success_add', $vars));
					otx_admin_log_action($this->_('text_success_add', $vars));
				}

				otx_router_redirect('index');
			}
		} else {
			if ($user_id) {
				$vars['user_name'] = $row['user_name'];
				$vars['display_name'] = $row['display_name'];
				$vars['email'] = $row['email'];
				$vars['group_id'] = $row['group_id'];
			} else {
				$vars['user_name'] = '';
				$vars['display_name'] = '';
				$vars['email'] = '';
				$vars['group_id'] = '';
			}

			$errors = array();
		}

		$form_wrapper = otx_html_create_wrapper()->set_form_errors($errors);
		$form_table = $form_wrapper->create_table($user_id ? $this->_('navtab_edit_title') : $this->_('navtab_add_title'), 'table-form');
		$form_table->add_form_entry($this->_('entry_user_name'), otx_html_form_text('user_name', $vars['user_name']));
		$form_table->add_form_entry($this->_('entry_display_name'), otx_html_form_text('display_name', $vars['display_name']));
		$form_table->add_form_entry($this->_('entry_password'), otx_html_form_text('password', null));

		$group_list = otx_db_fetch_pairs('SELECT group_id, group_name FROM #system_group# ORDER BY group_name');
		$form_table->add_form_entry($this->_('entry_group_id'), otx_html_form_select('group_id', $vars['group_id'], array('options' => $group_list)));

		$form_table->add_form_entry($this->_('entry_email'), otx_html_form_text('email', $vars['email']));

		otx_html_add_breadcrumb($this->_('breadcrumb_index'), otx_router_url('index'));
		if ($user_id) {
			otx_html_add_breadcrumb($this->_('breadcrumb_edit', $row));
			otx_html_response_title($this->_('breadcrumb_edit', $row));
			otx_html_add_navtab($this->_('navtab_edit_title'), $this->_('navtab_edit_desc'), otx_request_uri(), true);
		} else {
			otx_html_add_breadcrumb($this->_('breadcrumb_add'));
			otx_html_response_title($this->_('breadcrumb_add'));
			otx_html_add_navtab($this->_('navtab_index_title'), $this->_('navtab_index_desc'), otx_router_url('index'));
			otx_html_add_navtab($this->_('navtab_add_title'), $this->_('navtab_add_desc'), otx_router_url('add'), true);
		}
	}

	public function delete_action()
	{
		$user_id = otx_param_int('id');

		$sql = 'SELECT * FROM #system_user# WHERE user_id = ?';
		$row = otx_db_fetch_row($sql, $user_id);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $user_id));
			otx_router_redirect('index');
		}

		otx_db_delete('#system_user#', array('user_id' => $user_id));

		otx_admin_flash_message($this->_('text_success_delete', $row));
		otx_admin_log_action($this->_('text_success_delete', $row));
		otx_router_redirect('index');
	}

	public function activate_action()
	{
		$user_id = otx_param_int('id');

		$sql = 'SELECT * FROM #system_user# WHERE user_id = ?';
		$row = otx_db_fetch_row($sql, $user_id);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $user_id));
			otx_router_redirect('index');
		}

		$row['status'] = 1;
		otx_db_update('#system_user#', array('status' => $row['status']), array('user_id' => $user_id));
		otx_event_dispatch('user_activate', $row);

		otx_admin_flash_message($this->_('text_success_activate', $row));
		otx_admin_log_action($this->_('text_success_activate', $row));
		otx_router_redirect('index');
	}

	public function deactivate_action()
	{
		$user_id = otx_param_int('id');

		$sql = 'SELECT * FROM #system_user# WHERE user_id = ?';
		$row = otx_db_fetch_row($sql, $user_id);
		if (false == $row) {
			otx_admin_flash_warning($this->_('error_invalid_id', $user_id));
			otx_router_redirect('index');
		}

		$row['status'] = 0;
		otx_db_update('#system_user#', array('status' => $row['status']), array('user_id' => $user_id));
		otx_event_dispatch('user_deactivate', $row);

		otx_admin_flash_message($this->_('text_success_deactivate', $row));
		otx_admin_log_action($this->_('text_success_deactivate', $row));
		otx_router_redirect('index');
	}
}